<template>
	<view>
		<view class="tips color_fff size_12 align_c show">{{tipsText[0]}}</view>
		<view class="box-1" id="list-box">
			<view class="talk-list">
				<view v-for="(item,index) in talkList" :key="index">
					<view class="item flex_col" :class=" item.type == 1 ? 'push':'pull' ">
						<image :src="item.pic" mode="aspectFill" :data-type="item.type" @tap="toggleInfo" data-swit="open" class="pic"></image>
						<view class="content" v-html="item.content" v-if="!item.isimg"></view>
						<!-- <view class="content" v-html="item.content"></view> -->
						<view class="content" v-if="item.isimg">
							<image :src="item.content" mode="widthFix" @tap="previImages" class="mvimg"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="box-2">
			<view class="flex_col">
				<view class="flex_grow">
					<input type="text" class="content" v-model="content" placeholder="请输入聊天内容" placeholder-style="color:#DDD;" :cursor-spacing="6">
				</view>
				<!-- #ifdef APP-PLUS || H5 -->
				<button class="send" @tap="send">发送</button>
				<!-- #endif -->
				
				<!-- #ifdef MP-WEIXIN -->
				<button class="send" open-type="getUserInfo" @getuserinfo="getUserInfo">发送</button>
				<!-- #endif -->
			</view>
		</view>
		<view class="layer" v-if="show">
			<view class="layer-container">
				<view class="close" @tap="toggleInfo" data-swit="close">&#10005;</view>
				<view>昵称：小不点</view>
				<view>性别：男</view>
				<view>年龄：1岁</view>
				<view>星座：射手座</view>
				<view>爱好：睡觉</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				talkList:[],
				content:'',
				tipsText: ['发送“美女”给你分配一个女朋友！发送“能力”获取我会哪些技能'],
				text: true,
				imgurl: '',
				show: false,
				collect: [],
				avatar: '',
				// 分享相关
				share: {
					title: '输入“美女”点击发送分配女朋友',
					path: '/pages/index/index',
					imageUrl: uni.getStorageSync('shareImg'),
					desc: '',
					content:''
				}
			}
		},
		onLoad() {
			this.getMeinv()
		},
		methods: {
			// 机器人信息弹窗
			toggleInfo(e){
				let type = e.currentTarget.dataset.type
				let swit = e.currentTarget.dataset.swit
				if(type == 1){
					uni.navigateTo({
						url: '/pages/about/about'
					})
					return false
				}
				if(swit == 'open'){
					this.show = true
				}else if(swit = 'close'){
					this.show = false
				}
			},
			// 图片预览
			previImages(){
				uni.previewImage({
					current: this.imgurl,
					urls: [this.imgurl]
				})
			},
			// 设置页面滚动位置
			setPageScrollTo(selector){
				let view = uni.createSelectorQuery().in(this).select(selector);
				view.boundingClientRect((res) => {
					uni.pageScrollTo({
					    scrollTop:res.top - 30,	// -30 为多显示出大半个消息的高度，示意上面还有信息。
					    duration: 0
					});
				}).exec();
			},
			// 获取用户信息
			getUserInfo(){
				uni.getUserInfo({
					provider: "weixin",
					success: (resInfo) => {
						this.avatar = resInfo.userInfo.avatarUrl
						this.send()
					}
				})
			},
			// 发送信息
			send(){
				if(!this.content){
					uni.showToast({
						title:'请输入有效的内容',
						icon:'none'
					})
					return;
				}
				this.message(1, this.content)
				
				if(this.content == '美女'){
					this.getMeinv(1)
					return false
				}
				
				uni.request({
					url: 'https://api.tianapi.com/txapi/robot/index',
					data: {
						key: '1b9c3413d260a216a6395830bdfbaf34',
						question: this.content
					},
					success: (res) => {
						if(res.data.code == 200){
							this.text = true
							this.message(0, res.data.newslist[0].reply, false)
						}else {
							this.tipsText = res.data.msg;
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			getMeinv(type){
				let rand = parseInt(Math.random() * 1500)
				uni.request({
					url: '//api.tianapi.com/meinv/index',
					data: {
						key: '1b9c3413d260a216a6395830bdfbaf34',
						num: 1,
						page: rand
					},
					success: (res) => {
						if(res.data.code == 200){
							this.imgurl = res.data.newslist[0].picUrl
							this.text = false
							uni.setStorageSync('shareImg', this.imgurl)
							if(type){
								this.message(0, res.data.newslist[0].picUrl, true)
							}
						}else {
							this.tipsText = res.data.msg;
						}
					}
				})
			},
			message(type, content, isimg, avatar){
				if(type == 0){
					type = 0
					avatar = "/static/avatar.png"
				}
				// #ifdef MP-WEIXIN
				if(type == 1){
					type = 1
					avatar = this.avatar
				}
				// #endif
				// #ifdef APP-PLUS || H5
				if(type == 1){
					type = 1
					avatar = '/static/avatar.png'
				}
				// #endif
				
				let data = {
					"content": content,
					"type": type,
					"pic": avatar,
					"isimg": isimg
				}
				this.talkList.push(data)
				this.$nextTick(()=>{
					// 清空内容框中的内容
					this.content = '';
					uni.pageScrollTo({
						scrollTop: 9999999999,	// 设置一个超大值，以保证滚动条滚动到底部
						duration: 0
					});
				})
			}
		},
	}
</script>

<style lang="scss">
	@import "../../lib/global.scss";
	page{
		background-color: #F3F3F3;
		font-size: 28rpx;
	}
	
	/* 加载数据提示 */
	.tips{
		position: fixed;
		left: 0;
		top:var(--window-top);
		width: 100%;
		z-index: 9;
		background-color: rgba(0,0,0,0.4);
		height: 72rpx;
		line-height: 72rpx;
		transform:translateY(-80rpx);
		transition: transform 0.3s ease-in-out 0s;
		
		&.show{
			transform:translateY(0);
		}
	}
	
	.box-1{
		width: 100%;
		height: auto;
		padding-bottom: 100rpx;
		box-sizing: content-box;
		
		/* 兼容iPhoneX */
		margin-bottom: 0;  
		margin-bottom: constant(safe-area-inset-bottom);  
		margin-bottom: env(safe-area-inset-bottom);  
	}
	.box-2{
		position: fixed;
		left: 0;
		width: 100%;
		bottom: 0;
		/* #ifdef H5 */
		bottom: 100rpx;
		/* #endif */
		height: auto;
		z-index: 2;
		border-top: #e5e5e5 solid 1px;
		box-sizing: content-box;
		background-color: #F3F3F3;
		
		/* 兼容iPhoneX */
		padding-bottom: 0;  
		padding-bottom: constant(safe-area-inset-bottom);  
		padding-bottom: env(safe-area-inset-bottom);  
		
		>view{
			padding: 0 20rpx;
			height: 100rpx;
		}
		
		.content{
			background-color: #fff;
			height: 64rpx;
			padding: 0 20rpx;
			border-radius: 32rpx;
			font-size: 28rpx;
		}
		
		.send{
			background-color: #42b983;
			color: #fff;
			height: 64rpx;
			margin-left: 20rpx;
			border-radius: 32rpx;
			padding: 0;
			width: 120rpx;
			line-height: 62rpx;
			
			&:active{
				background-color: #5fc496;
			}
		}
	}
	
	.talk-list{
		padding-bottom: 20rpx;
		
		/* 消息项，基础类 */
		.item{
			padding: 20rpx 20rpx 0 20rpx;
			align-items:flex-start;
			align-content:flex-start;
			color: #333;
			
			.pic{
				width: 92rpx;
				height: 92rpx;
				border-radius: 50%;
				border: #fff solid 1px;
			}
			
			.content{
				padding: 20rpx;
				border-radius: 4px;
				max-width: 500rpx;
				word-break: break-all;
				line-height: 52rpx;
				position: relative;
			}
			
			/* 收到的消息 */
			&.pull{
				.content{
					margin-left: 32rpx;
					background-color: #fff;
					
					&::after{
						content: '';
						display: block;
						width: 0;
						height: 0;
						border-top: 16rpx solid transparent;
						border-bottom: 16rpx solid transparent;
						border-right: 20rpx solid #fff;
						position: absolute;
						top: 30rpx;
						left: -18rpx;
					}
				}
			}
			
			/* 发出的消息 */
			&.push{
				/* 主轴为水平方向，起点在右端。使不修改DOM结构，也能改变元素排列顺序 */
				flex-direction: row-reverse;
				
				.content{
					margin-right: 32rpx;
					background-color: #a0e959;
					
					&::after{
						content: '';
						display: block;
						width: 0;
						height: 0;
						border-top: 16rpx solid transparent;
						border-bottom: 16rpx solid transparent;
						border-left: 20rpx solid #a0e959;
						position: absolute;
						top: 30rpx;
						right: -18rpx;
					}
				}
			}
		}
	}
	.mvimg{
		width: 400rpx;
	}
	.layer-container{
		padding: 40rpx;
		width: 400rpx;
		line-height: 45rpx;
		background-color: #fff;
		border-radius: 20rpx;
		font-size: 32rpx;
	}
	.close{
		position: absolute;
		top: 20rpx;
		right: 20rpx;
	}
</style>